﻿using Cinemachine;
using DG.Tweening;
using Scenario;
using UI;
using UnityEngine;

namespace Core
{
    public class Scene2 : MonoBehaviour
    {
        [Header("小B")]
        public string spawnXiaoBScenarioName;
        public int spawnXiaoBScenarioId;
        public GameObject xiaoBPrefab;
        [Header("小C")] 
        public NeedItemInfo xiaoCNeed;
        public TriggerEventListener xiaoCTrigger;
        public string xiaoCScenario;
        public int spawnXiaoCScenarioId;
        public int[] spawnXiaoCShakeIdArr;
        public GameObject xiaoCPrefab;
        
        private Camera m_mainCamera;
        private CinemachineBrain m_cinemachineBrain;
        private void OnEnable()
        {
            m_mainCamera = Camera.main;
            m_cinemachineBrain = m_mainCamera.GetComponent<CinemachineBrain>();
            
            UI_ScenarioPanel.OnScenarioStep += OnScenarioStep;
            xiaoCTrigger.OnPlayerEnter += OnPlayerEnter;
        }
        private void OnDisable()
        {
            UI_ScenarioPanel.OnScenarioStep -= OnScenarioStep;
            xiaoCTrigger.OnPlayerEnter -= OnPlayerEnter;
        }

        //回调
        private void OnScenarioStep(ScenarioData scenarioData, int id)
        {
            if (scenarioData.Name == spawnXiaoBScenarioName && id == spawnXiaoBScenarioId)
            {
                var playerRoot = PlayerController.Root;
                var role = GameObject.Instantiate(xiaoBPrefab, playerRoot.position + Vector3.right * 1.75f, Quaternion.identity, GameManager.SceneRoot).transform;
            
                var delta = playerRoot.position - role.position;
                role.Find("Model").GetComponent<SpriteRenderer>().flipX = delta.x < 0;
            
                role.GetComponent<InteractRole>().SetFollow(PlayerController.FollowTarget);
                PlayerController.AddFollowed(role);

                role.DOLocalMoveY(1.5f, 0.25f).SetRelative(true).SetLoops(2, LoopType.Yoyo);
            }
            else if (scenarioData.Name == xiaoCScenario)
            {
                if (id == spawnXiaoCScenarioId)
                {
                    var playerRoot = PlayerController.Root;
                    var role = GameObject.Instantiate(xiaoCPrefab, playerRoot.position + Vector3.right * 1.75f, Quaternion.identity, GameManager.SceneRoot).transform;
                
                    var delta = playerRoot.position - role.position;
                    role.Find("Model").GetComponent<SpriteRenderer>().flipX = delta.x < 0;
                
                    role.GetComponent<InteractRole>().SetFollow(PlayerController.FollowTarget);
                    PlayerController.AddFollowed(role);

                    role.DOLocalMoveY(1.5f, 0.25f).SetRelative(true).SetLoops(2, LoopType.Yoyo);
                }
                
                for (var i = spawnXiaoCShakeIdArr.Length - 1; i >= 0; i--)
                {
                    if (spawnXiaoCShakeIdArr[i] == id)
                    {
                        m_cinemachineBrain.enabled = false;
                        m_mainCamera.DOShakePosition(1f, 0.25f).OnComplete(() =>
                        {
                            m_cinemachineBrain.enabled = true;
                        });
                        break;
                    }
                }
            }
        }
        private void OnPlayerEnter()
        {
            if (!xiaoCNeed.Check(false)) return;
            UI_ScenarioPanel.StartScenario(xiaoCScenario);
            xiaoCTrigger.gameObject.SetActive(false);
        }
    }
}